package com.iticit.controller;

import com.alibaba.fastjson.JSON;
import com.iticit.api.send.service.CopsService;
import com.iticit.api.vo.bescanned.CopsBeScannedReqVo;
import com.iticit.api.vo.bescanned.CopsBeScannedResVo;
import com.iticit.api.vo.closeorder.CopsCloseOrderReqVo;
import com.iticit.api.vo.closeorder.CopsCloseOrderResVo;
import com.iticit.api.vo.openid.CopsOpenIdFetchReqVo;
import com.iticit.api.vo.openid.CopsOpenIdFetchResVo;
import com.iticit.api.vo.payquery.CopsPayOrderQueryReqVo;
import com.iticit.api.vo.payquery.CopsPayOrderQueryResVo;
import com.iticit.api.vo.preorder.CopsPreOrderReqVo;
import com.iticit.api.vo.preorder.CopsPreOrderResVo;
import com.iticit.api.vo.refund.CopsRefundReqVo;
import com.iticit.api.vo.refund.CopsRefundResVo;
import com.iticit.api.vo.refundquery.CopsRefundQueryReqVo;
import com.iticit.api.vo.refundquery.CopsRefundQueryResVo;
import com.iticit.api.vo.wx.minipro.CopsMiniProReqVo;
import com.iticit.api.vo.wx.minipro.CopsMiniProResVo;
import com.iticit.config.CopsPropertyConfig;
import com.iticit.util.FileHelper;
import com.iticit.util.SM2SignUtils;
import com.iticit.util.SM4Utils;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/cops", produces = {"application/json;charset=UTF-8"})
public class CopsTestController {

    @Resource
    CopsService copsService;
    @Resource
    CopsPropertyConfig copsPropertyConfig;

    @PostMapping("/test/preOrderJH")
    public CopsPreOrderResVo preOrderSend(@RequestBody CopsPreOrderReqVo copsPreOrderReqVo) {
        return copsService.preOrderSend(copsPreOrderReqVo);
    }

    @PostMapping("/test/beScannedJH")
    public CopsBeScannedResVo beScannedSend(@RequestBody CopsBeScannedReqVo copsBeScannedReqVo) {
        return copsService.beScannedSend(copsBeScannedReqVo);
    }

    @PostMapping("/test/closeJH")
    public CopsCloseOrderResVo closeSend(@RequestBody CopsCloseOrderReqVo copsCloseOrderReqVo) {
        return copsService.closeOrderSend(copsCloseOrderReqVo);
    }

    @PostMapping("/test/findOrderJH")
    public CopsPayOrderQueryResVo findOrderSend(@RequestBody CopsPayOrderQueryReqVo copsPayOrderQueryReqVo) {
        return copsService.orderQuerySend(copsPayOrderQueryReqVo);
    }

    @PostMapping("/test/refundJH")
    public CopsRefundResVo refundSend(@RequestBody CopsRefundReqVo copsRefundReqVo) {
        return copsService.refundSend(copsRefundReqVo);
    }

    @PostMapping("/test/findRefOrderJH")
    public CopsRefundQueryResVo findRefOrderSend(@RequestBody CopsRefundQueryReqVo copsRefundQueryReqVo) {
        return copsService.refundQuerySend(copsRefundQueryReqVo);
    }

    @PostMapping("/test/openIdFetchJH")
    public CopsOpenIdFetchResVo openIdFetchSend(@RequestBody CopsOpenIdFetchReqVo copsOpenIdFetchReqVo) {
        return copsService.openIdFetchSend(copsOpenIdFetchReqVo);
    }

    @PostMapping("/test/unifiedorderJH")
    public CopsMiniProResVo unifiedorderSend(@RequestBody CopsMiniProReqVo copsMiniProReqVo) {
        return copsService.miniProSend(copsMiniProReqVo);
    }

    /**
     * 不太一样 异步通知的话  你们得先用你们的公钥解密encryptKey 得到sm4的密钥 再进行报文解密
     */
    @PostMapping("/test/decryptAndVerySign")
    public String decryptAndVerySign(@RequestBody GfbNotifyDTO notifyDTO) throws Exception {
        if (Objects.isNull(notifyDTO)) {
            return "";
        }
        //转义
        notifyDTO.setSignature(notifyDTO.getSignature().replaceAll("\\\\", ""));
        notifyDTO.setEncryptData(notifyDTO.getEncryptData().replaceAll("\\\\", ""));

        //1.用私钥解密encryptKey得到sm4的密钥
        String sm4Secret = SM2SignUtils.decryptString(
                FileHelper.loadFileBytes(copsPropertyConfig.getKeyBasePath(), copsPropertyConfig
                        .getPrivateKey()), notifyDTO.getEncryptkey(), copsPropertyConfig.getEncoding());
        //2.报文解密
        String responseDecryptJson = SM4Utils
                .SM4DecryptData(sm4Secret, notifyDTO.getEncryptData(), copsPropertyConfig.getEncoding());
//        boolean veriyfyResult = SM2SignUtils
//                .verifySignString(FileHelper.loadFileBytes(copsPropertyConfig.getKeyBasePath(), copsPropertyConfig
//                                .getCompanyPublicKey()), notifyDTO.getSignature(), responseDecryptJson,
//                        copsPropertyConfig.getEncoding());
//        log.info("verifying signature of response report is {}", veriyfyResult);
//        if (!veriyfyResult) {
//            throw new BusinessException(WholeConstants.CopsMsg.VERIFY_SIGN_NOT_PASS);
//        }
        // 验签通过即返回解密后的报文
        return responseDecryptJson;
    }

    @PostMapping("/test/encryptNotifyResult")
    public String encryptNotifyResult(String requestJsonReport) throws Exception {
        Map<String, String> headerMap = copsService.buildHttpHeaderMap(requestJsonReport);
        headerMap.remove("requestEncryptJson");
        return JSON.toJSONString(headerMap);
    }
}
